package com.magima.utils;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Environment;
import android.provider.ContactsContract;
import android.telephony.SmsManager;

public class MobileOperator {
	public static final int PICK_CONTACT = 11;
	
	public static void call(Context context, String number){
		if(number != null && !number.equals("")){
			Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + number));
			context.startActivity(intent);
		}
	}

	public static void toSMS(Context context, String number, String text){
		if(number != null){
			Uri uri = Uri.parse("smsto:" + number);            
			Intent intent = new Intent(Intent.ACTION_SENDTO, uri);            
			intent.putExtra("sms_body", text);            
			context.startActivity(intent); 
		}
	}
	public static void toEmail(Context context, String address, String subject, String text, String chooserTitle){
		if(address != null && !address.equals("")){
			Intent intent = new Intent(Intent.ACTION_SEND); 
			intent.setType("message/rfc822"); 
			intent.putExtra(Intent.EXTRA_EMAIL, new String[]{address}); 
			intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
			intent.putExtra(Intent.EXTRA_TEXT, text); 
			context.startActivity(Intent.createChooser(intent, chooserTitle));
		}
	}
	
	public static void sendSMS(String phoneNumber, String message){
       SmsManager sms = SmsManager.getDefault();
       sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
	
	public static boolean checkNetAvailable(Context context){
		ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo wifiInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		
		if(wifiInfo.isAvailable() || mobileInfo.isAvailable()){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean checkNetConnected(Context context){
		ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo wifiInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		NetworkInfo mobileInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		
		if(wifiInfo.isConnected() || mobileInfo.isConnected()){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean checkWIFIAvailable(Context context){
		ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo wifiInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		
		if(wifiInfo.isAvailable()){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean checkWIFIConnected(Context context){
		ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo wifiInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		
		if(wifiInfo.isConnected()){
			return true;
		}else{
			return false;
		}
	}
	
	public static boolean checkExternalStorageWritable(){
		return Environment.getExternalStorageDirectory().canWrite();
	}
	
	public static void callContactPickActivity(Context context){
		Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
		((Activity)context).startActivityForResult(intent, PICK_CONTACT);
	}
	
	public static void callGalleryActivity(Context context, File file){
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri mUri = Uri.fromFile(file);
        intent.setDataAndType(mUri, "image/*");
        context.startActivity(intent);
	}
}
